<?php

/**
 * @author DeineAgentur UG (haftungsbeschränkt) 
 * @www http://www.deineagentur.eu/
 * @copyright 2009
 */


class DA_Hash {
    
    function getHash ($passwd){
        global $allowed_alternativ_hash;
        
        if (function_exists('skein_hash_hex')) {
            
            return 'skein$XAMPP$'.skein_hash_hex($passwd, 1024);
            
        } else {
        
            $hash_algos = hash_algos();
            
            if ( in_array('whirlpool',$hash_algos) && in_array('whirlpool',$allowed_alternativ_hash) ) {
                $use_algo = 'whirlpool';
            } elseif ( in_array('gost',$hash_algos) && in_array('gost',$allowed_alternativ_hash) ) {
                $use_algo = 'gost';
            } elseif ( in_array('sha512',$hash_algos) && in_array('sha512',$allowed_alternativ_hash) ) {
                $use_algo = 'sha512';
            } elseif ( in_array('sha256',$hash_algos) && in_array('sha256',$allowed_alternativ_hash) ) {
                $use_algo = 'sha256';
            } elseif ( in_array('ripemd256',$hash_algos) && in_array('ripemd256',$allowed_alternativ_hash) ) {
                $use_algo = 'ripemd256';
            } elseif ( in_array('sha1',$hash_algos) && in_array('sha1',$allowed_alternativ_hash) ) {
                $use_algo = 'sha1';
            } else {
                $use_algo = 'md5';
            }
            
            return $use_algo.'$XAMPP$'.hash($use_algo, $passwd);
        }
    }
    
    function checkHash ($passwd,$hash){
        global $allowed_alternativ_hash;
        
        if (function_exists('skein_hash_hex')) {
            
            $hash_to_check = "skein$".skein_hash_hex($passwd, 1024);
            
        } else {
        
            $hash_algos = hash_algos();
            
            if ( in_array('whirlpool',$hash_algos) && in_array('whirlpool',$allowed_alternativ_hash) ) {
                $use_algo = 'whirlpool';
            } elseif ( in_array('gost',$hash_algos) && in_array('gost',$allowed_alternativ_hash) ) {
                $use_algo = 'gost';
            } elseif ( in_array('sha512',$hash_algos) && in_array('sha512',$allowed_alternativ_hash) ) {
                $use_algo = 'sha512';
            } elseif ( in_array('sha256',$hash_algos) && in_array('sha256',$allowed_alternativ_hash) ) {
                $use_algo = 'sha256';
            } elseif ( in_array('ripemd256',$hash_algos) && in_array('ripemd256',$allowed_alternativ_hash) ) {
                $use_algo = 'ripemd256';
            } elseif ( in_array('sha1',$hash_algos) && in_array('sha1',$allowed_alternativ_hash) ) {
                $use_algo = 'sha1';
            } else {
                $use_algo = 'md5';
            }
            
            $hash_to_check = $use_algo."$".hash($use_algo, $passwd);
        }
        
        if ($hash_to_check == $hash) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
	
	
}

?>